package cn.wjc.demo.parttwo.controller;

import cn.wjc.demo.common.entity.Partner;
import cn.wjc.demo.common.entity.Student;
import cn.wjc.demo.common.page.PageQuery;
import cn.wjc.demo.parttwo.mapper.IPartnerMapper;
import cn.wjc.demo.parttwo.service.IPartnerService;
import cn.wjc.demo.parttwo.service.IStudentService;
import cn.wjc.demo.parttwo.feign.PartoneFeignService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.seata.core.context.RootContext;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

@RestController
@RequestMapping("/parttwo")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ParttwoController {

    private final IPartnerMapper partnerMapper;

    private final PartoneFeignService partoneFeignService;

    private final IStudentService studentService;

    private final IPartnerService partnerService;

    @GetMapping("/printString/{s}")
    public String test(@PathVariable String s ) {
        System.out.println("远程调用中...");
        partoneFeignService.parttowPrint(s);
        return "远程调用partone成功";
    }

    @PostMapping("/doSomething")
    public void doSomething(@RequestParam("s") String s) {
        String str = "这是parttwo中写死的字符串";
        System.out.println(s);
    }

    @PostMapping("/add/student")
    public void addStudent(@RequestBody Student student) {
        System.out.println("seata全局事务id====================>"+ RootContext.getXID());
        System.out.println("正在被partthree远程调用中...");
        System.out.println("执行【新增学生】的请求...");
        studentService.addStudent(student);
    }

    @GetMapping("/queryListPage/partner")
    public IPage<Partner> queryListPagePartner(PageQuery pageQuery, Partner partner) {
        return partnerService.queryPartner(pageQuery, partner);
    }

    @GetMapping("/partnerList")
    public void partnerList() {
        Partner partner = new Partner();
        partner.setPName("李莉莉");
        //int insert = partnerMapper.insert(partner);
        LambdaQueryWrapper<Partner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(partner.getPName()), Partner::getPName, partner.getPName());
        List<Partner> list = partnerMapper.selectList(queryWrapper);
        System.out.println(list);
    }

    @PostMapping("/testReflect")
    public void testReflect() {

        try {
            Partner partner = Partner.class.getDeclaredConstructor().newInstance();
            partner.setPName("阿萨德上");
            System.out.println(partner);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
